<?php
/**
 * Created by PhpStorm.
 * User: dev
 * Date: 2018/8/7
 * Time: 10:46
 */

namespace App\Services\ApiServer\Response;

use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\DB;
use App\Services\ApiServer\Response\Node\HomeNode;

/**
 * 资产
 */
class Currency extends BaseResponse implements InterfaceResponse
{
    /**
     * 接口名称
     * @var string
     */
    protected $method = 'currency';

    /**
     * 获取基础配置信息
     * @param  array &$params 请求参数
     * @return array
     */
    public function run(&$params)
    {
        //查询config表
        return [
            'status' => true,
            'code' => '200',
            'data' => [
                'current_time' => date('Y-m-d H:i:s')
            ]
        ];
    }

    /**
     * 资产列表
     * @DateTime   2018-08-10
     * @version   [version]
     * @param    {string}
     * @return  [type]     [description]
     */
    public function currencyList()
    {

    }

    /**
     * 同步资产列表
     * @DateTime   2018-08-10
     * @version   [version]
     * @param    {string}
     * @return  [type]     [description]
     */
    public function syncCurrencyList()
    {

    }

    /**
     * 首页关注资产列表
     * @DateTime 2018-08-10
     * @version  [version]
     * @param    {string}
     * @return  [type]     [description]
     */
    public function currencyConcernList()
    {
        //查询地址数量
        $param = Request::all();
        $walletType = $param['wallet_type'];
        $address = $param['address'];
        //查询用户关注的资产
        $where= [];
        $where['is_show'] = 1;
        $where['wallet_type_id'] = $walletType;
        $currencyConcernRows = Db::table('currency as c')->leftJoin('currency_concern as cc','c.currency_id','=','cc.currency_id')->select('c.currency_id','c.currency_name','c.en_name','c.currency_logo','c.currency_mark','c.sort','c.precision','c.contract_address','c.wallet_type_id','c.precision_flag')->where($where)->get()->toArray();
        //查询数量
        $walletNode = new HomeNode('cam');
        foreach ($currencyConcernRows as $currKey => &$currVal) {
            $amount = 0;
            //合约地址不为空查询数量
            if(!empty($currVal['contract_address'])){
                $amount = $walletNode->getWalletBalance($currVal['wallet_type_name'],$address,$currVal['contract_address']);
            }
            $currVal['coin'] = $amount;
            //查询资产价格
            $icoClass = new Quotes();
            $currencyPrice = $icoClass->currencyPrice($currVal['currency_id'],'','','');
            if(!isset( $currencyPrice['price'])){
                Log::write('请求行情api数据出错,资产id'.$val['currency_id'],'error');
                $currencyPrice['price'] = '-';
                $currencyPrice['sign'] = '$';
            }
            $currVal['last_currency_price'] = $currencyPrice['price'];
            $currVal['last_currency_symbol'] =  $currencyPrice['sign'];
            if($currVal['precision_flag'] == 1){
                if($currencyPrice['price'] == '-'){
                    $currVal['last_asset'] = 0;
                }else{
                    $currVal['last_asset'] = $amount * $currencyPrice['price'] * pow(10,-$currVal['precision']);
                }
            }else{
                 if($currencyPrice['price'] == '-'){
                    $currVal['last_asset'] = 0;
                }else{
                    $currVal['last_asset'] = $amount * $currencyPrice['price'];
                }
            }
        }
        $this->apiSuccess('获取首页资产列表成功',$currencyConcernRows);
    }
}